<?php 
    namespace Media\Repository;
    
    class PhotoRepository extends AbstractRepository {
        protected  $entity="Media\\Entity\\Photo";
        


        public function listLimit($albumId, $number=null){
        	$dql = "SELECT c FROM $this->entity c JOIN c.album u WHERE u.id = ?1 order by c.id desc";
        	$query = $this->getEntityManager()->createQuery($dql);
        	$query->setParameter(1, $albumId);
        	if($number!=null && $number>0) $query->setMaxResults($number);
        	return $query->getResult();
        }
        
        public function listFromMin($minId, $maxResult=null, $excludeMin=true){
            $quantifier = $excludeMin?">":">=";
            $dql = "SELECT c FROM $this->entity c WHERE c.id ".$quantifier." ?1 order by c.id asc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $minId);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listFromMax($maxId, $maxResult=null, $excludeMax=true){
            $quantifier = $excludeMax?"<":"<=";
            $dql = "SELECT c FROM $this->entity c WHERE c.id ".$quantifier." ?1 order by c.id desc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $maxId);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        public function listFromMinInAlbum($albumId, $minId, $maxResult=null, $excludeMin=true){
            $quantifier = $excludeMin?">":">=";
            $dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.album)= ?1 AND c.id ".$quantifier." ?2 order by c.id asc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $albumId)
                ->setParameter(2, $minId);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listFromMaxInAlbum($albumId, $maxId, $maxResult=null, $excludeMax=true){
            $quantifier = $excludeMax?"<":"<=";
            $dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.album)= ?1 AND c.id ".$quantifier." ?2 order by c.id desc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $albumId)
                ->setParameter(2, $maxId);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listByProvider($idProvider, $maxResult=null){
            $dql = "SELECT c FROM $this->entity c WHERE c.provider_id = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idProvider);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listByFilter($idFilter, $maxResult=null){
            $dql = "SELECT c FROM $this->entity c WHERE c.filter_id = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idFilter);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listByUser($idUser, $maxResult=null){
            $dql = "SELECT c FROM $this->entity c WHERE c.user_uid = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idUser);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listByAlbum($idAlbum, $maxResult=null){
            $dql = "SELECT c FROM $this->entity c WHERE c.album_id = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idAlbum);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        public function countByAlbum($albumId){
            $dql = "SELECT count(c.id) as nb FROM $this->entity c WHERE IDENTITY(c.album) = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $albumId);
            $query->setMaxResults(1);
            $results = $query->getResult();
            return (($results==null)||(count($results)==0))?0:$results[0]["nb"];
        }
        
        
    }
